﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace com.ubosi.spider.hotel
{
    public class BaseSpider
    {
        protected spider.core.Net.NetSpider _NetSpider;
        protected config.SpiderConfig _SpiderConfig;

        /// <summary>
        /// 本地文件名，为空从网络抓取，不为空，直接读取该文件返回,主要用于离线调试
        /// </summary>
        public string LocalFileName
        {
            get;
            set;
        }
        
        public BaseSpider(spider.core.Net.NetSpider netSpider,spider.config.SpiderConfig spiderConfig)
        {
            this._NetSpider = netSpider;
            this._SpiderConfig = spiderConfig;
        }
        /// <summary>
        /// 得到HTML
        /// </summary>
        /// <param name="url">网络URL</param>
        /// <param name="encoding">编码</param>
        /// <returns></returns>
        private string GetHtml(string url,Encoding encoding)
        {
            if (_NetSpider == null)
                return string.Empty;
            try
            {
                if (string.IsNullOrEmpty(LocalFileName))
                {
                    return _NetSpider.GetHtmlDocument(url, encoding);
                }
                else
                {
                    return _NetSpider.GetHtmlFromFile(LocalFileName, encoding);
                }
            }
            catch (Exception ex)
            {
                return string.Empty;
            }
        }
        /// <summary>
        /// 得到HTML
        /// </summary>
        /// <param name="url">网络URL</param>
        /// <param name="encoding">编码</param>
        /// <returns></returns>
        public string GetHtmlDocument(string url, Encoding encoding)
        {
            string htmlData = string.Empty;
            for (int i = 0; i < _NetSpider.RequestSettings.TryCount;i++ )
            {
                htmlData = GetHtml(url, encoding);
                if (!string.IsNullOrEmpty(htmlData))
                    return htmlData;
            }
            return htmlData;
        }
        /// <summary>
        /// 得到XML
        /// </summary>
        /// <param name="url">网络URL</param>
        /// <returns></returns>
        public string GetXmlDocument(string url)
        {
            if (_NetSpider == null)
                return string.Empty;
            if (!string.IsNullOrEmpty(LocalFileName))
            {
                return _NetSpider.GetXmlDocument(url);
            }
            else
            {
                return _NetSpider.GetHtmlFromFile(LocalFileName, Encoding.UTF8);
            }
        }
    }
}
